package NWPackage;

import java.util.HashMap;
import java.util.List;
import java.util.Set;


public class AgentsDistMatrix {
	
	
	private HashMap<String,CandidatesDistMatrix> agents;

	public AgentsDistMatrix()
	{
		this.agents = new HashMap<String, CandidatesDistMatrix>(100);
	}
	

	public void addAgentProbs(String agent, List<ProbabilityCell> agentProbs) {
		CandidatesDistMatrix newCandidatesDisMatrix = new CandidatesDistMatrix();
		newCandidatesDisMatrix.calcRangeVotesProbs(agentProbs);
		
		this.agents.put(agent, newCandidatesDisMatrix);
		
	}

	public double getVoteProb(String agentName, String candidate, int vote) {
		return this.agents.get(agentName).getVoteProb(candidate,vote);
	}


	public void addEmptyAgent(String agentName, String[] candidates,int numOfVotes) {
		CandidatesDistMatrix newCandidatesDisMatrix = new CandidatesDistMatrix();
		newCandidatesDisMatrix.initial(candidates,numOfVotes);
		this.agents.put(agentName, newCandidatesDisMatrix);
	}


	public int getNumberOfVotes(String agent, String candidate) {
		return this.agents.get(agent).getNumberOfVotes(candidate);
	}


	public void fixRank(String agent, String candidate, int i) {
		this.agents.get(agent).fixRanks(candidate,i);
		
	}
	public Set<String> getVoters() {
		return this.agents.keySet();
	}


	public void addEmptyAgent(String agentName, int numCandidates, int numVotes) {
		CandidatesDistMatrix newCandidatesDisMatrix = new CandidatesDistMatrix();
		newCandidatesDisMatrix.initial(numCandidates,numVotes);
		this.agents.put(agentName, newCandidatesDisMatrix);
		
	}


	public boolean isAgentExist(String agentName) {
		return agents.containsKey(agentName);
	}


	public void addVotesToAgent(String agentName, String candidate,	List<String> votes) {
		if (!isAgentExist(agentName))
		{
			CandidatesDistMatrix newCandidatesDisMatrix = new CandidatesDistMatrix();
			newCandidatesDisMatrix.add(candidate,votes);
			this.agents.put(agentName, newCandidatesDisMatrix);
		}
		else
		{
			//agent is already exist so need to add the votes to his list
			CandidatesDistMatrix candidatesDisMatrix = agents.get(agentName);
			candidatesDisMatrix.add(candidate,votes);
		}
		
	}


	public List<String> getCandidatesList() {
		Set<String> allAgents = this.agents.keySet();
		return agents.get(allAgents.iterator().next()).getCandidatesList();
	}


	public HashMap<String,RankDist> getAgentAndCandidatesVotes(String agent,String candidate) {
		HashMap<String,RankDist> ans = new HashMap<String, RankDist>();
		ans.put(candidate,this.agents.get(agent).getAllRankDist(candidate)); 
		return ans; 
	}


	public void SetAgentProbsFromOther(String agent,HashMap<String, RankDist> allProbsIsWinner) {
		this.agents.get(agent).add(allProbsIsWinner);;
		
		
	}


	public double getSumOfVotes(String agent, String cand) {
		return agents.get(agent).getAllRankDist(cand).getSumProbs();
	}
	
	public AgentsDistMatrix clone()
	{
		HashMap<String,CandidatesDistMatrix> newAgents = new HashMap<String, CandidatesDistMatrix>();
		for (String agent : this.agents.keySet()) {
			newAgents.put(agent, this.agents.get(agent).clone());
		}
		AgentsDistMatrix mat = new AgentsDistMatrix();
		mat.agents = newAgents;
		return mat;
	}

}
